package com.example.page;


import com.example.objects.SortedListOf;
import com.example.objects.User;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import java.util.List;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

public class AdminPage extends InternalPage{

    @FindBy(xpath = "//div[2]/table/tbody/tr")
    public List<WebElement> usersTable;

    @FindBy(id = "username")
    public WebElement loginField;

    @FindBy(name = "email")
    public WebElement emailField;

    @FindBy(id = "password")
    public WebElement passwordField;

    @FindBy(id = "password2")
    public WebElement password2Field;

    @FindBy(name = "submit")
    public WebElement saveButton;

    private SortedListOf<User> cachedUsers;

    public AdminPage deleteUserByName(User user) throws Exception {
        for (WebElement row: usersTable)
        {
            if (user.equals(convertRowToUser(row)))
            {
                if (row.findElements(By.cssSelector("img[src$=\"delete.png\"]")).size()>0)
                {
                    row.findElement(By.cssSelector("img[src$=\"delete.png\"]")).click();
                    wait_().until(alertIsPresent()).accept();
                    break;
                }
                else throw new Exception("Пользователя "+ user +" нельзя удалить");
            }
        }
        cachedUsers = null;
        return MyPageFactory.getPage(driverFirefox, AdminPage.class);
    }

    public boolean isUserExist(User user) {
        for(WebElement row: usersTable)
        {
            if (user.equals(convertRowToUser(row)))
                return true;
        }
        return false;
    }

    public boolean isDeletedUser(User user) {
        for(WebElement row: usersTable)
        {
            if (user.equals(convertRowToUser(row)))
            {
            if (row.findElements(By.cssSelector("img[src$=\"delete.png\"]")).size()>0)
                return true;
            }
        }
        return false;
    }

    public AdminPage createNewUser(User user) {
        loginField.sendKeys(user.name);
        emailField.sendKeys(user.email);
        passwordField.sendKeys(user.password);
        password2Field.sendKeys(user.password);
		saveButton.click();
        cachedUsers = null;
        return MyPageFactory.getPage(driverFirefox, AdminPage.class);
    }

    public SortedListOf<User> getUsers() {
        if (cachedUsers == null)
        {
            cachedUsers = new SortedListOf<User>();
            for (WebElement row: usersTable){
                cachedUsers.add(convertRowToUser(row));
            }
        }
        return new SortedListOf<User>(cachedUsers);
    }

    public int getUserCount(){
        return usersTable.size();
    }

    private User convertRowToUser(WebElement row) {
        List<WebElement> cells = row.findElements(By.tagName("td"));
        return new User().withName(cells.get(1).getText())
                .withEmail(cells.get(2).getText())
                .withRole(cells.get(3).getText());
    }
}
